package co.yixiang.yshop.module.crm.controller.admin.basestorage;

import co.yixiang.yshop.framework.apilog.core.annotation.ApiAccessLog;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageParam;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.module.crm.controller.admin.basestorage.vo.*;
import co.yixiang.yshop.module.crm.dal.dataobject.basestorage.BaseStorageDO;
import co.yixiang.yshop.module.crm.service.basestorage.BaseStorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static co.yixiang.yshop.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 仓库")
@RestController
@RequestMapping("/crm/storage")
@Validated
public class BaseStorageController {

    @Resource
    private BaseStorageService storageService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库")
//    @PreAuthorize("@ss.hasPermission('crm:storage:create')")
    public CommonResult<Integer> createStorage(@Valid @RequestBody BaseStorageSaveReqVO createReqVO) {
        return success(storageService.createStorage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库")
//    @PreAuthorize("@ss.hasPermission('crm:storage:update')")
    public CommonResult<Boolean> updateStorage(@Valid @RequestBody BaseStorageSaveReqVO updateReqVO) {
        storageService.updateStorage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('crm:storage:delete')")
    public CommonResult<Boolean> deleteStorage(@RequestParam("id") Integer id) {
        storageService.deleteStorage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
//    @PreAuthorize("@ss.hasPermission('crm:storage:query')")
    public CommonResult<BaseStorageRespVO> getStorage(@RequestParam("id") Integer id) {
        BaseStorageDO storage = storageService.getStorage(id);
        return success(BeanUtils.toBean(storage, BaseStorageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库分页")
//    @PreAuthorize("@ss.hasPermission('crm:storage:query')")
    public CommonResult<PageResult<BaseStorageRespVO>> getStoragePage(@Valid BaseStoragePageReqVO pageReqVO) {
        PageResult<BaseStorageDO> pageResult = storageService.getStoragePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseStorageRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得仓库列表")
//    @PreAuthorize("@ss.hasPermission('crm:storage:query')")
    public CommonResult<List<BaseStorageRespVO>> getStorageList(@Valid BaseStoragePageReqVO pageReqVO) {
        List<BaseStorageDO> list = storageService.getStorageList(pageReqVO);
        return success(BeanUtils.toBean(list, BaseStorageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('crm:storage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStorageExcel(@Valid BaseStoragePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseStorageRespVO> list = storageService.getStorageListForExport(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "仓库.xls", "数据", BaseStorageRespVO.class, list);
    }

} 